package com.xxl.rpc.core.remoting.provider;

import com.xxl.rpc.core.remoting.net.params.XxlRpcStatusTemplate;
import org.springframework.util.StringUtils;

/**
 * 自己定义服务异常
 * <p>留给业务系统子类实现</p>
 *
 * @author zhanpu
 * @date 2021/4/11
 */
public class XxlRpcProviderException extends RuntimeException {

    private static final long serialVersionUID = -7031897150745766933L;

    /**
     * 状态码
     */
    private int status;

    /**
     * 响应编码
     */
    private String returnCode;

    /**
     * {@link XxlRpcStatusTemplate}
     */
    private XxlRpcStatusTemplate xxlRpcStatusTemplate;

    public XxlRpcProviderException() {
        super();
    }

    public XxlRpcProviderException(int status, String returnCode, String message) {
        super(message);
        this.status = status;
        this.returnCode = returnCode;
    }

    public XxlRpcProviderException(XxlRpcStatusTemplate xxlRpcStatusTemplate) {
        super(xxlRpcStatusTemplate.getMsg());
        this.status = xxlRpcStatusTemplate.getStatus();
        this.returnCode = xxlRpcStatusTemplate.toString();
        this.xxlRpcStatusTemplate = xxlRpcStatusTemplate;
    }

    public XxlRpcProviderException(String message, XxlRpcStatusTemplate xxlRpcStatusTemplate) {
        super(StringUtils.isEmpty(message) ? xxlRpcStatusTemplate.getMsg() : message);
        this.status = xxlRpcStatusTemplate.getStatus();
        this.returnCode = xxlRpcStatusTemplate.toString();
        this.xxlRpcStatusTemplate = xxlRpcStatusTemplate;
    }

    public int getStatus() {
        return status;
    }

    public String getReturnCode() {
        return returnCode;
    }

    public XxlRpcStatusTemplate getXxlRpcStatusTemplate() {
        return xxlRpcStatusTemplate;
    }
}
